package cn.zx.service.impl;

import cn.zx.dao.AccountDao;
import cn.zx.domain.Account;
import cn.zx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账户的业务层实现类
 *
 * 事务控制应该都是在业务层
 */
@SuppressWarnings("all")
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS)//开始事务支持 且配置事务通知的属性
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);

    }
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        System.out.println("transfer....");
        //2.1根据名称查询转出账户
        Account sourceAccount = accountDao.findAccountByName(sourceName);
        //2.2根据名称查询转入账户
        Account targetAccount = accountDao.findAccountByName(targetName);
        //2.3转出账户减钱
        sourceAccount.setMoney(sourceAccount.getMoney()-money);
        //2.4转入账户加钱
        targetAccount.setMoney(targetAccount.getMoney()+money);
        //2.5更新转出账户
        accountDao.updateAccount(sourceAccount);
        int i=1/0;
        //2.6更新转入账户
        accountDao.updateAccount(targetAccount);
    }
}
